<template>
  <div class="app-container">
    <div class="dict">
      <div class="left">
        <div class="top">
          <el-input
            v-model="queryParams.search"
            size="small"
            placeholder="输入关键字进行过滤"
            clearable
            @clear="getList"
            @keyup.enter.native="getList"
          ></el-input>
        </div>
        <div class="middle">
          <div
            v-for="(item ,index) in typeList"
            :key="item.id"
            class="m_list"
            :class="{ 'activeColor' : index === checkIndex}"
            @mouseenter="mouseOver(index)"
            @click="handleClick(index ,item)"
            @mouseleave="mouseLeave"
          >
            <div class="l_txt">{{ item.dictName }}</div>
            <div v-if="curIndex !== index" class="r_txt">{{ item.dictType }}</div>
            <div v-else class="r_txt">
              <el-tooltip
                effect="dark"
                content="编辑字典类型"
                placement="top"
              >
                <i style="padding-right:8px" class="el-icon-edit" @click.stop="handleUpdate(item)"></i>
              </el-tooltip>
              <el-tooltip
                effect="dark"
                content="删除字典类型"
                placement="top"
              >
                <i class="el-icon-delete" @click.stop="handleDelete(item)"></i>
              </el-tooltip>
            </div>
          </div>
        </div>
        <div class="bottom">
          <el-button size="mini" @click="handleAdd()">新增</el-button>
          <el-button size="mini" @click="getList">刷新</el-button>
        </div>
      </div>
      <div class="right">
        <div class="r_btns">
          <div>
            <el-input
              v-model="queryParams.dictLabel"
              placeholder="字典标签"
              clearable
              style="width:280px; margin-right:12px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
            <el-button type="primary" size="small" @click="handleQuery">搜索</el-button>
          </div>
          <div>
            <el-button size="small" type="primary" @click="handleAddDict">新增</el-button>
            <el-button size="small" type="info" @click="handleClearCache">清理缓存</el-button>
            <el-button size="small" :disabled="single" type="info" @click="handleDeleteDict">删除</el-button>
          </div>
        </div>
        <div class="tableBox">
          <el-table
            v-loading="loading"
            :data="dataList"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="字典编码" prop="id" />
            <el-table-column label="字典标签" prop="dictLabel" />
            <el-table-column label="字典键值" prop="dictValue" />
            <el-table-column label="字典排序" prop="sort" />
            <el-table-column label="是否默认" prop="is_default">
              <template slot-scope="scope">
                <el-switch
                  disabled
                  :value="scope.row.is_default"
                />
              </template>
            </el-table-column>
            <el-table-column
              label="状态"
              prop="status"
              :formatter="statusFormat"
            />
            <el-table-column
              label="备注"
              prop="remark"
              :show-overflow-tooltip="true"
            />
            <el-table-column label="创建时间" prop="create_datetime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.create_datetime) }}</span>
              </template>
            </el-table-column>
            <el-table-column
              v-if="hasPermi(['system:dict:type:{id}:put','system:dict:type:{id}:delete'])"
              label="操作"
              width="100px"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  v-hasPermi="['system:dict:type:{id}:put']"
                  size="mini"
                  type="text"
                  @click="handleUpdateDict(scope.row)"
                >修改</el-button>
                <el-button
                  v-hasPermi="['system:dict:type:{id}:delete']"
                  size="mini"
                  type="text"
                  @click="handleDeleteDict(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getDicTypeList"
          />
        </div>
      </div>
    </div>
    <!-- 新增 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="form.dictName" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="form.dictType" placeholder="请输入字典类型" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="isopen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典类型">
          <el-input v-model="form.dictType" :disabled="true" />
        </el-form-item>
        <el-form-item label="数据标签" prop="dictLabel">
          <el-input v-model="form.dictLabel" placeholder="请输入数据标签" />
        </el-form-item>
        <el-form-item label="数据键值" prop="dictValue">
          <el-input v-model="form.dictValue" placeholder="请输入数据键值" />
        </el-form-item>
        <el-form-item label="显示排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="是否默认" prop="is_default">
          <el-switch
            v-model="form.is_default"
            active-text="是"
            inactive-text="否"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listType, getType, delType, addType, updateType, clearCache } from "@/api/vadmin/system/dict/type";
import { addData, delData, getData, listData, updateData } from "@/api/vadmin/system/dict/data";
export default {
  name: "Dict",
  data() {
    return {
      dataList: [],
      dataQueryParams: {},
      dicFilterText: "",
      checkIndex: 0,
      curIndex: "",
      dictId: "",
      isDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isopen: false,
      // 状态数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 1000000,
        dictName: undefined,
        dictType: undefined,
        status: undefined,
        search: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dictName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" }
        ],
        dictType: [
          { required: true, message: "字典类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  async  created() {
    await this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    if (!(this.dictId && this.queryParams.dictType)) {
      this.queryParams.dictType = this.typeList[0].dictType;
      this.dictId = this.typeList[0].id;
      await this.getDicTypeList();
    }
  },
  methods: {
    handleClick(index, item) {
      this.checkIndex = index;
      this.dictId = item.id;
      this.queryParams.dictType = item.dictType;
      this.getDicTypeList();
    },
    // 移入
    mouseOver(index) {
      this.curIndex = index;
    },
    mouseLeave() {
      this.curIndex = "";
    },
    selectionChange() {

    },
    /** 查询字典类型列表 */
    async  getList() {
      this.loading = true;
      this.queryParams.dictType = "";
      await listType(this.queryParams).then(response => {
        this.typeList = response.data.results;
        this.total = response.data.count;
        this.loading = false;
      }
      );
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    handleCancel() {
      this.isopen = false;
      this.resetDict();
    },
    // 表单重置
    resetDict() {
      this.form = {
        id: undefined,
        dictLabel: undefined,
        dictValue: undefined,
        sort: 0,
        status: this.selectDictDefault(this.typeOptions),
        remark: undefined
      };
      this.resetForm("form");
    },
    // 取消字典类型按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dictName: undefined,
        dictType: undefined,
        status: this.selectDictDefault(this.statusOptions),
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getDicTypeList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加字典类型";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      // isDisabled
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getType(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改字典类型";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateType(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addType(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除字典编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delType(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      });
    },
    //  --------------------右边----------------
    /** 查询字典数据列表 */
    getDicTypeList() {
      this.loading = true;
      listData(this.queryParams).then((response) => {
        this.dataList = response.data.results;
        this.total = response.data.count;
        this.loading = false;
      });
    },
    /** 新增按钮操作 */
    handleAddDict() {
      this.reset();
      this.isopen = true;
      this.title = "添加字典数据";
      this.form.dictType = this.queryParams.dictType;
    },
    /** 提交按钮 */
    handleSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateData(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.isopen = false;
              this.getDicTypeList();
            });
          } else {
            this.form.dict_data = this.dictId;
            addData(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.isopen = false;
              this.getDicTypeList();
            });
          }
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdateDict(row) {
      this.reset();
      const id = row.id || this.ids;
      getData(id).then((response) => {
        this.form = response.data;
        this.isopen = true;
        this.title = "修改字典数据";
      });
    },
    /** 删除按钮操作 */
    handleDeleteDict(row) {
      const dictCodes = row.id || this.ids;
      this.$confirm('是否确认删除字典编码为"' + dictCodes + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delData(dictCodes);
      })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 清理缓存按钮操作 */
    handleClearCache() {
      clearCache().then((response) => {
        this.msgSuccess("清理成功");
      });
    }

  }
};
</script>

<style lang="scss" scoped>
.app-container{
  padding: 0 !important;
  border: 1px solid #F4F7F9;
  .tableBox{
    margin: 0;
    border-top: none;
    padding: 0 12px;
  }
}
.dict{
  display: flex;
  background: #F4F7F9;
  .left{
    flex:0 0 300px;
    position: relative;
    height: calc(100vh - 108px);
    background: #fff;
    .top{
      border-bottom: 1px solid #e7e7e7;
      padding: 13px 15px;
    }
    .middle{
     overflow-y: auto;
    height: calc( 100vh - 225px);
      .m_list{
        display: flex;
        height: 36px;
        font-size: 12px;
        padding: 0 24px;
        justify-content: space-between;
        align-items: center;
        cursor: pointer;
        .l_txt{
          color: #666666;
        }
         .r_txt{
          color: #999999;
        }
      }
      .activeColor{
         background: #e6eff8;
      }
    }
     .m_list:hover{
          background: #e6eff8;
        }
    .bottom{
      display: flex;
      justify-content: space-between;
      position: absolute;
      bottom: 0;
      width: 300px;
      z-index: 99;
      padding: 13px;
      height: 56px;
      border-top: 1px solid #ccc;
    }
  }
  .right{
     flex: 1;
     min-width: 800px;
     overflow-x: auto;
     padding-right: 20px;
     margin-left: 12px;
     background: #fff;
    .r_btns{
      padding: 13px 15px;
      display: flex;
      justify-content: space-between;
      border-bottom: 1px solid #e7e7e7;
    }
  }
}

/*修改滚动条样式*/
.left ::-webkit-scrollbar{
  width:3px;
  height:10px;
  /**/
}
.left ::-webkit-scrollbar-track{
  background: rgb(239, 239, 239);
  border-radius:2px;
}
.left ::-webkit-scrollbar-thumb{
  background: #bfbfbf;
  border-radius:10px;
}
.left ::-webkit-scrollbar-thumb:hover{
  background: #333;
}
.left ::-webkit-scrollbar-corner{
  background: #179a16;
}
</style>
